package com.mk.settlement.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.google.common.collect.Maps;
import com.mk.settlement.common.ExcelExport;
import com.mk.settlement.common.Page;
import com.mk.settlement.model.SettlementOrder;
import com.mk.settlement.service.StatisticaOrderService;

/**
 * Created by nolan on 15/11/9. description:
 */
@Controller
@RequestMapping("/sc/rpt/order")
public class StatisticaOrderController {
	private Logger log = LoggerFactory.getLogger(StatisticaOrderController.class);
	@Autowired
	private StatisticaOrderService statisticaOrderService;

	@RequestMapping(value = "/statistica", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> pageQuery(SettlementOrder sorder,Page page ) {
//		System.out.println(ToStringBuilder.reflectionToString(page));
//		System.out.println("BeginTime=="+page.getBeginTime()+"    EndTime="+page.getEndTime());
//		System.out.println(ToStringBuilder.reflectionToString(sorder));
		log.info("订单基础数列表查询开始：");
		log.info("查询参数（SettlementOrder）："+ToStringBuilder.reflectionToString(sorder));
		log.info("查询参数（Page）："+ToStringBuilder.reflectionToString(page));
		Page p = statisticaOrderService.findData(sorder, page);
		log.info("查询结束，共查询到："+p.getTotalRecord()+"条数据！");
		Map<String, Object> rtnMap = Maps.newHashMap();
		rtnMap.put("result", p.getResult());
		rtnMap.put("page", p);
		return rtnMap;
	}

	@RequestMapping(value = "/export", method = RequestMethod.POST)
	@ResponseBody
	public void exportExcel( HttpServletRequest request,
			HttpServletResponse response,
			SettlementOrder sorder,Page page) {
		log.info("订单纬度列表导出开始：");
		log.info("导出参数（Page）："+ToStringBuilder.reflectionToString(page));
		log.info("导出参数（SettlementOrder）："+ToStringBuilder.reflectionToString(sorder));
		String excelName = "订单基础数据";
		ExcelExport.exprotBefore(request, response, excelName);
		OutputStream fOut = null;
		try {
			String[] titile = { "订单号", "原始金额", "金额", "费用类型", "结算类型", "销售周期",
					"统计状态", "结算方名称", "创建时间", "结账日期" };
			List<String[]> listData= statisticaOrderService.findExportData( titile.length,sorder, page);
			log.info("准备导出"+listData.size()+"条数据！");
			Boolean[] isdigit = { null, true, true, null, null, null, null, null, null, null };
			int[] columnWidth = { 35, 28, 28, 35, 28, 25, 33, 45, 60, 60 };
			HSSFWorkbook workbook = ExcelExport.export(excelName, titile,
					listData, columnWidth, isdigit);
			fOut = response.getOutputStream();
			workbook.write(fOut);
		} catch (UnsupportedEncodingException e1) {
			log.error("导出失败:"+e1.getMessage());
			e1.getMessage();
		} catch (Exception e) {
			log.error("导出失败:"+e.getMessage());
			e.getMessage();
		} finally {
			try {
				fOut.flush();
				fOut.close();
			} catch (IOException e) {
				log.error("导出失败:"+e.getMessage());
				e.getMessage();
			}
		}
		log.info("导出完成");
//		System.out.println("文件生成...");
	}

	@RequestMapping(value = "/editSettlementSum", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> editSettlementSum( 
			@RequestParam(value = "id", required = true) @RequestBody Long id,
			@RequestParam(value = "settlementSum", required = true) @RequestBody BigDecimal settlementSum) {
//		 System.out.println(" editSettlementSum----id=="+id+" settlementSum=="+settlementSum);
		log.info("修改结算金额开始："); 
		log.info("传入参数是：id="+id+",新的金额是："+settlementSum);
		Map<String, Object> rtnMap = Maps.newHashMap();
		int rs=statisticaOrderService.editSettlementSum(id, settlementSum);
		log.info("修改完成，共成功修改"+rs+"条数据！");
		rtnMap.put("result", rs);
		return rtnMap;
	}

}